<template>
  <view class="container">
    <view class="form">
      <text class="title">注册</text>
      <input class="input" v-model="username" placeholder="请输入用户名" />
      <input class="input" type="password" v-model="password" placeholder="请输入密码" />
      <input class="input" type="password" v-model="confirmPassword" placeholder="请确认密码" />
      <button class="btn" type="primary" @click="handleRegister">注册</button>
      <view class="link">
        <text>已有账号？</text>
        <navigator url="/pages/index/index" open-type="navigate">去登录</navigator>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      username: '',
      password: '',
      confirmPassword: ''
    };
  },
  methods: {
    handleRegister() {
      if (!this.username || !this.password || !this.confirmPassword) {
        uni.showToast({
          title: '请输入完整信息',
          icon: 'none'
        });
        return;
      }
      if (this.password !== this.confirmPassword) {
        uni.showToast({
          title: '两次输入的密码不一致',
          icon: 'none'
        }); 
        return;
      }
	  uni.showLoading({
	  	title:'注册中...'
	  })
	  uniCloud.callFunction({
	  	name:'addUser',
		data:{
			'username':this.username,
			'password':this.password
		},
		success: (msg) => {
			console.log("addUser callback"+JSON.stringify(msg));
			setTimeout(()=>{
				uni.hideLoading()
				uni.showToast({
					title:'注册成功'
				})
				uni.navigateTo({
				  url: '/pages/index/index'
				});
			},1000)
		},
		fail: (error) => {
			console.error("addUser fail"+error)
		}
	  })
    }
  }
};
</script>

<style>
.container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100vh;
  background-color: #f5f5f5;
}
.form {
  display: flex;
  flex-direction: column;
  width: 80%;
  background-color: #fff;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
.title {
  font-size: 20px;
  font-weight: bold;
  text-align: center;
  margin-bottom: 20px;
}
.input {
  margin-bottom: 15px;
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 4px;
}
.btn {
  margin-top: 10px;
  width: 100%;
  padding: 0;
}
.link {
  display: flex;
  justify-content: center;
  margin-top: 15px;
}
.link text {
  margin-right: 5px;
}
.link navigator {
  color: #007AFF;
}
</style>